package com.aoxon.cargo.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.SystemMessage;
import com.aoxon.cargo.loader.FileLoader;

/**
 * 系统信息适配器
 * 
 * @author ZZJ
 * 
 */
public class SupMessageListAdapter extends BaseAdapter implements MyAdapter{

	private boolean isBusy = false;

	private List<SystemMessage> messageList;
	private LayoutInflater layoutInflater;

	private FileLoader myImageLoader;

	public SupMessageListAdapter(Context context,
			List<SystemMessage> messageList) {
		this.messageList = messageList;
		this.layoutInflater = LayoutInflater.from(context);
		myImageLoader = new FileLoader(100, 100, false);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public void resetList(List<?> list) {
		// TODO Auto-generated method stub
		if(list != null){
			this.messageList.clear();
			this.messageList = (List<SystemMessage>) list;
		}
		this.notifyDataSetChanged();
	}
	
	@Override
	public void setBusy(boolean isBusy) {
		// TODO Auto-generated method stub
		this.isBusy = isBusy;
	}

	@Override
	public void clearCache() {
		// TODO Auto-generated method stub
		if (myImageLoader != null) {
			myImageLoader.clearCache();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return messageList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return messageList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;

		// 获取控件
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(
					R.layout.sup_system_message_list_item, null);
			viewHolder.ivMessagePicture = (ImageView) convertView
					.findViewById(R.id.ivSystemMessageListPicture);
			viewHolder.tvMessageTitle = (TextView) convertView
					.findViewById(R.id.tvSystemMessageListText);
			viewHolder.tvMessageTime = (TextView) convertView
					.findViewById(R.id.tvSystemMessageListTime);
			viewHolder.progressBar = (ProgressBar) convertView
					.findViewById(R.id.pbSystemManagerListItemLoading);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		// 绑定数据
		try {
			SystemMessage systemMessage = messageList.get(position);
			String path = systemMessage.getPicture().getPictureUrl() + "s.png";

			viewHolder.tvMessageTitle.setText(systemMessage.getTitle());
			viewHolder.tvMessageTime.setText(systemMessage.getPostTime());
			viewHolder.ivMessagePicture.setImageResource(R.drawable.loading);
			if (!isBusy) {
				myImageLoader.displayImage(path, viewHolder.ivMessagePicture,
						viewHolder.progressBar, false);
			} else {
				myImageLoader.displayImage(path, viewHolder.ivMessagePicture,
						viewHolder.progressBar, true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			viewHolder.progressBar.setVisibility(View.GONE);
		}

		return convertView;
	}

	class ViewHolder {
		ImageView ivMessagePicture;
		TextView tvMessageTitle;
		TextView tvMessageTime;
		ProgressBar progressBar;
	}
}
